window.onload = init;

/*
 * Loading sequence
 */
var standbyAnimation = [
	[ 0, function () {
			document.getElementById("top1").className += " _2357purple1";
			document.getElementById("standby1").style.display = "block";
		}
	],
	[ 100, function () {
			document.getElementById("top2").className += " _2357yellow2";
			document.getElementById("standby2").style.display = "";
		}
	],
	[ 100, function () {
			document.getElementById("top3").className += " _2357blue3";
			document.getElementById("standby3").style.display = "";
		}
	],
	[ 100, function () {
			document.getElementById("top4").className += " _2357blue1";
			document.getElementById("standby4").style.display = "";
		}
	],
	[ 100, function () {
			document.getElementById("menuItem1").style.display = "block";
			document.getElementById("standby5").style.display = "";
		}
	],
	[ 100, function () {
			document.getElementById("menuItem2").style.display = "block";
			document.getElementById("standby6").style.display = "";
		}
	],
	[ 100, function () {
			document.getElementById("menuItem3").style.display = "block";
			document.getElementById("footer").style.position = "";
			document.getElementById("footer").style.bottom = "";
		}
	],
	[ 1600, function () { document.getElementById("standbyGreetings").style.display = "none";  } ],
	[ 100, function () { document.getElementById("ssItem1").style.display = "";  } ],
	[ 100, function () {
			document.getElementById("ssgap1").style.display = "";
			document.getElementById("ssgap1").className = "gap _2357yellow1 blink";
		}
	],
	[ 100, function () {
			document.getElementById("ssStatus1").style.display = "";
			document.getElementById("ssgap1").className = "gap _2357yellow1";
		}
	],
	[ 100, function () {
			document.getElementById("ssItem2").style.display = "";
			document.getElementById("ssgap2").style.display = "";
			document.getElementById("ssgap2").className = "gap _2357purple1 blink";
		}
	],
	[ 100, function () {
			document.getElementById("ssStatus2").style.display = "";
			document.getElementById("ssgap2").className = "gap _2357purple1";
		}
	],
	[ 100, function () {
			document.getElementById("ssItem3").style.display = "";
			document.getElementById("ssgap3").style.display = "";
			document.getElementById("ssgap3").className = "gap _2357blue2 blink";
		}
	],
	[ 100, function () {
			document.getElementById("ssStatus3").style.display = "";
			document.getElementById("ssgap3").className = "gap _2357blue2";
		}
	],
	[ 100, function () {
			document.getElementById("ssItem4").style.display = "";
			document.getElementById("ssgap4").style.display = "";
			document.getElementById("ssgap4").className = "gap _2357purple3 blink";
			document.getElementById("ssStatus4").style.display = "";
		}
	],
	[ 500, function () {
			document.getElementById("ssStatus4").innerHTML = "Complete";
			document.getElementById("ssgap4").className = "gap _2357purple3";
		}
	],
	[ 100, function () {
			document.getElementById("ssItem5").style.display = "";
			document.getElementById("ssgap5").style.display = "";
			document.getElementById("ssgap5").className = "gap _2357yellow1 blink";
		}
	],
	[ 100, function () {
			document.getElementById("ssStatus5").style.display = "";
			document.getElementById("ssgap5").className = "gap _2357yellow1";
		}
	],
	[ 100, function () {
			document.getElementById("ssItem6").style.display = "";
			document.getElementById("ssgap6").style.display = "";
			document.getElementById("ssgap6").className = "gap _2357yellow1 blink";
		}
	],
	[ 100, function () {
			document.getElementById("ssStatus6").style.display = "";
			document.getElementById("ssgap6").className = "gap _2357yellow1";
		}
	],
	[ 1600, function () {
			document.getElementById("startSequence").style.display = "none";
			document.getElementById("accessGranted").style.display = "";
		}
	],
	[ 100, function () { document.getElementById("online").style.color = ""; } ],
	[ 1600, function () {
			document.getElementById("standby").style.display = "none";
			document.getElementById("missionWatch").style.display = "";
		}
	],
];

function Animate(animation) {
	var index = 0;

	setTimeout(doIt, animation[index][0]);

	function doIt () {
		animation[index][1]();

		index++;

		if (index < animation.length)
			setTimeout(doIt, animation[index][0]);
	}
}

/*
 * Page controls and navigation
 */
var bkgColorClasses = ["_2357yellow1", "_2357yellow2", "_2357yellow3", "_2357purple1", "_2357purple2", "_2357purple3", "_2357purple4", "_2357blue1", "_2357blue2", "_2357blue3"];

function show(id) {
	document.getElementById("credits").style.display = 'none';
	document.getElementById("missionWatch").style.display = 'none';
	document.getElementById("objects").style.display = 'none';

	document.getElementById(id).style.display = "block";

	if (id == "missionWatch")
		DisplayMissionList();
}


/*
 * Stardate
 */
var timerID = 0;
function stardate() {
	now = new Date();
	nowMonth = now.getMonth();
	nowDate = now.getDate();
	nowYear = now.getYear() + 1900;
	nowHour = now.getHours();
	nowMinute = now.getMinutes();
	nowSecond = now.getSeconds();
	now = null;

	var Elapsed = nowSecond + (60 * nowMinute) + (3600 * nowHour) + (86400 * (nowDate - 1));

	if (nowMonth > 10) {var Elapsed = Elapsed + (86400*334);}
	else if (nowMonth > 9) {var Elapsed = Elapsed + (86400*304);}
	else if (nowMonth > 8) {var Elapsed = Elapsed + (86400*273);}
	else if (nowMonth > 7) {var Elapsed = Elapsed + (86400*243);}
	else if (nowMonth > 6) {var Elapsed = Elapsed + (86400*212);}
	else if (nowMonth > 5) {var Elapsed = Elapsed + (86400*181);}
	else if (nowMonth > 4) {var Elapsed = Elapsed + (86400*151);}
	else if (nowMonth > 3) {var Elapsed = Elapsed + (86400*120);}
	else if (nowMonth > 2) {var Elapsed = Elapsed + (86400*90);}
	else if (nowMonth > 1) {var Elapsed = Elapsed + (86400*59);}
	else if (nowMonth > 0) {var Elapsed = Elapsed + (86400*31);}
	if (nowYear > 2100) {var nowYear = nowYear-1900;}

	var thisyear = Math.round(Elapsed / 315.36) / 100;
	var lpyra = Math.round(nowYear/400);
	var lpyrb = nowYear/400;

	if (lpyra == lpyrb)
		if (nowMonth > 2)
			var Elapsed = Elapsed + (86400);

	var issue = Math.round(((nowYear-2323)/100)-.5);
	var yearspast = (nowYear - (2323+(issue * 100))) * 1000
	var total = thisyear+yearspast
	if (nowMinute < 10) {var mns="0"} else {var mns=""}
	if (nowSecond < 10) {var scs="0"} else {var scs=""}
	var Temp2= '[' + issue + ']  '+ total + '    ' + nowHour + ':' + mns + nowMinute + ':'+ scs + nowSecond;

	document.getElementById("stardate").innerHTML = Temp2;

	timerID = setTimeout("stardate()",1000);
}


/*********************
 * MISSION WATCH
 *********************/
var missionsObj = {};

function init() {
	GetTargetObjectList();

	var vis = new SpaceVisualizer(document.getElementById("visCanvas"));
	vis.addBody({ x: 200, y: 100 });
	vis.addBody({ x: 250, y: 150 });
	vis.addBody({ x: 350, y: 70 });
	vis.render();

	Animate(standbyAnimation);
	document.getElementById("top1").className += " _2357purple1";
	document.getElementById("top2").className += " _2357yellow2";
	document.getElementById("top3").className += " _2357blue3";
	document.getElementById("top4").className += " _2357blue1";
	document.getElementById("menuItem1").style.display = "block";
	document.getElementById("menuItem2").style.display = "block";
	document.getElementById("menuItem3").style.display = "block";
	//document.getElementById("standby").style.display = "none";
	//document.getElementById("missionWatch").style.display = "";
	DisplayMissionList();
}

function GetTargetObjectList() {
	var xhReq = new XMLHttpRequest();
	xhReq.open("GET", "missionWatch.json", false);
	xhReq.send(null);
	missionsObj = JSON.parse(xhReq.responseText);
}

function DisplayMissionList() {
	var missionListDiv = document.getElementById('spacecraftsList');
	missionListDiv.innerHTML = "";
	var missions = missionsObj["missions"];

	for (var i = 0; i < missions.length ; i++) {
		var optDiv = document.createElement('div');
		optDiv.setAttribute("onclick", "displayInfo(" + missions[i].id + ")");
		optDiv.className = "optbtn " + bkgColorClasses[Math.floor( Math.random() * bkgColorClasses.length )];
		optDiv.innerHTML = "<span>" + missions[i].name;

		missionListDiv.appendChild(optDiv);
	}
}

function GetAstroidInfo() {
	var astroId = document.getElementById("AstroidID").value;

	if (astroId == null || astroId == "")
	{
		alert("You need to provide a value.");
	}

	// Call interface to get information

	// Display information on page
}

function displayInfo(id) {
	var missions = missionsObj["missions"];
	var target = null;
	missions.forEach(function (mission) {
		if (mission["id"] == id) 
			target = mission;
	});

	if (target == null)
		return;
	
	document.getElementById("target-id").innerHTML = target["id"];
	document.getElementById("target-name").innerHTML = target["name"];
	document.getElementById("target-desc").innerHTML = target["description"];
	document.getElementById("target-launch").innerHTML = target["launch"];

	document.getElementById("target-pos").innerHTML = "(loading...)";
	getCartesian(target["id"], function (json) {
		var xyz = json[0]["xyz"].split(" ");
		document.getElementById("target-pos").innerHTML = "X: " + xyz[0] + "<br />Y: " + xyz[1] + "<br />Z: " + xyz[2];
	});

	var links = "";
	if (target["links"] != null) {
		target["links"].forEach(function (link) {
			links += "<a href=\"" + link["url"] + "\">" + link["name"] + "</a><br />";
		});
		document.getElementById("target-links").innerHTML = links;
	}
}


function getCartesian(id, callback) {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", "horizon?fn=cartesian&id=" + id, true);
	xhr.onreadystatechange = done;
	xhr.send(null);

	function done() {
		if (this.readyState == 4 && this.status == 200) {
			var json = JSON.parse(xhr.responseText);
			callback(json);
		}
	}
}


function SpaceVisualizer(canvas) {
	var g = canvas.getContext("2d");
	var bodies = [];


	this.addBody = function (body) {
		bodies.push(body);
	};


	this.render = function () {
		bodies.forEach(function (body) {
			g.beginPath();
			g.arc(body.x, body.y, 10, 0, 2 * Math.PI);
			g.strokeStyle = "#FFF";
			g.stroke();
		});
	};
}